package cn.xique.smartlock.web.front;


import com.jfinal.core.paragetter.Para;
import com.jfinal.kit.LogKit;
import com.jfinal.kit.Ret;
import io.jboot.utils.EncryptCookieUtils;
import io.jboot.utils.StrUtils;
import io.jboot.web.controller.JbootController;
import io.jboot.web.controller.annotation.RequestMapping;
import io.jpress.JPressConsts;
import io.jpress.JPressOptions;
import io.jpress.commons.sms.SmsKit;
import io.jpress.module.smLock.model.LockUser;
import io.jpress.module.smLock.service.LockUserService;

import javax.inject.Inject;
import java.util.Date;

@RequestMapping("/SmartLock/User")
public class UserController extends JbootController {

    @Inject
    private LockUserService lockUserService;


    public void SendSmsCode() {
        String phone = getPara("phone");
        if (StrUtils.isBlank(phone)) {
            renderJson(Ret.fail().set("message", "手机号不能为空..."));
            return;
        }

        if (StrUtils.isMobileNumber(phone) == false) {
            renderJson(Ret.fail().set("message", "你输入的手机号码不正确"));
            return;
        }


        String code = SmsKit.generateCode();
        String template = JPressOptions.get("reg_sms_validate_template");
        String sign = JPressOptions.get("reg_sms_validate_sign");

        Boolean sendOk = SmsKit.sendCode(phone, code, template, sign);

        if (sendOk) {
            renderJson(Ret.ok().set("message", "短信发送成功，请手机查看"));
        } else {
            renderJson(Ret.fail().set("message", "短信实发失败，请联系管理员"));
        }

    }



    //    手机号登录系统  手机验证码登录系统
    public void doLogin(@Para("phone") String phone, @Para("code") String code) {

        if (StrUtils.isBlank(phone) || StrUtils.isBlank(code)) {
            LogKit.error("你当前的 idea 或者 eclipse 可能有问题，请参考文档：http://www.jfinal.com/doc/3-3 进行配置");
            return;
        }

        if (StrUtils.isMobileNumber(phone) == false) {
            renderJson(Ret.fail().set("message", "你输入的手机号码不正确"));
            return;
        }
        LockUser lockUser = new LockUser();
        lockUser.setPhone(phone);
        lockUser.setLogintime(new Date());
        //判断验证码是否有效Ret ret
        Ret ret = Ret.create();
        if(SmsKit.validateCode(phone, code)){
            //查询是否存在

            LockUser lockUser1 = lockUserService.findByPhone(phone);
            if(lockUser1 != null){
                lockUser.setId(lockUser1.getId());
                lockUserService.update(lockUser);
            }else{
                lockUserService.save(lockUser);
                lockUser1 = lockUserService.findByPhone(phone);
            }
            ret.set("user_id", lockUser1.getId()).setOk();
        }else{
            ret.setFail();
        }

        if (ret.isOk()) {
            EncryptCookieUtils.put(this, JPressConsts.COOKIE_UID, ret.getLong("user_id"));
        }

        renderJson(ret);
    }


    /**
     * 账号退出
     * */
    /**
     * 退出登录
     */
    public void doLogout() {
        EncryptCookieUtils.remove(this, JPressConsts.COOKIE_UID);
        renderJson(Ret.ok());
    }
}
